package com.ttai.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerVerifyAcountComponet;
import com.ttai.dagger.module.activity.VerifyAcountPresenterModule;
import com.ttai.model.dao.bean.Acount_Item;
import com.ttai.model.net.VerifyAccountBean;
import com.ttai.model.net.VerifyIdBean;
import com.ttai.presenter.activity.InsertIdAcountPresenter;
import com.ttai.presenter.activity.InsertNewAcountPresenter;
import com.ttai.presenter.activity.InsertTipAcountPresenter;
import com.ttai.presenter.activity.VerifyAccountPresenter;
import com.ttai.ui.adapter.VerifyIdRecycleViewAdapter;
import com.ttai.ui.adapter.VerifyLoginRecycleViewAdapter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.untils.Constant;
import com.ttai.untils.ItemClickSupport;
import com.ttai.untils.getui.DemoIntentService;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VerifyLogin extends BaseActivity {


    ArrayList<VerifyAccountBean.SellectListBean> accountArrayList = new ArrayList<>();
    ArrayList<VerifyIdBean.SellectListBean> idArrayList = new ArrayList<>();
    @Bind(R.id.detail_recycler_view)
    RecyclerView detailRecyclerView;
    @Bind(R.id.btn_verify)
    Button btnVerify;
    private String index;
    private VerifyLoginRecycleViewAdapter verifyLoginRecycleViewAdapter;
    private VerifyIdRecycleViewAdapter verifyIdRecycleViewAdapter;


    @Inject
    VerifyAccountPresenter verifyAccountPresenter;


    public String flag;
    //    private String loginOrotp;
    private String data = "";
    private String token;
    private String cid;

    public int checkamount = 0;
    public ArrayList<String> checkedList = new ArrayList<>();
    public ArrayList<String> checkedAcountList = new ArrayList<>();
    public ArrayList<String> checkedNameList = new ArrayList<>();
    public ArrayList<Integer> checkedPositionList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verifysecurity);
        ButterKnife.bind(this);
        setComponet();


        Intent intent = getIntent();
        index = intent.getStringExtra("index");
        flag = intent.getStringExtra("flag");


        switch (index) {
            case "0": {
                accountArrayList = (ArrayList<VerifyAccountBean.SellectListBean>) intent.getSerializableExtra("accoutnList");
                detailRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));

                verifyLoginRecycleViewAdapter = new VerifyLoginRecycleViewAdapter(this, accountArrayList);
                detailRecyclerView.setAdapter(verifyLoginRecycleViewAdapter);

                ItemClickSupport.addTo(detailRecyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {

                        System.out.println("我点击了" + position);

                        accountArrayList.get(position).setCheckedFlag(!accountArrayList.get(position).getCheckedFlag());

                        System.out.println("name:" + accountArrayList.get(position).getAccountname() + "flag" + accountArrayList.get(position).getCheckedFlag());

                        if (checkamount < 3) {
                            if (accountArrayList.get(position).getCheckedFlag() == true) {
                                v.setBackgroundResource(R.drawable.encryption_choice);
//                                checkedPositionList.add(position);
                                checkedList.add(accountArrayList.get(position).getAccountname());
                                checkamount++;
                            } else {
                                v.setBackgroundColor(Color.WHITE);
//                                checkedPositionList.remove(position);
                                checkedList.remove(accountArrayList.get(position).getAccountname());
                                checkamount--;
                            }
                            Toast.makeText(VerifyLogin.this, "已选择" + checkedList.size() + "项,最多选择三项", Toast.LENGTH_SHORT).show();
                        } else {
//                            for (int i = 0; i < checkedPositionList.size(); i++) {
//                                System.out.println("position:" + checkedPositionList.get(i));
//                                System.out.println("点击POSI" + position);
//                                if (position == checkedPositionList.get(i)) {
//                                    checkedPositionList.remove(position);
//                                    v.setBackgroundColor(Color.WHITE);
//                                    checkedList.remove(accountArrayList.get(position).getAccountname());
//                                    checkamount--;
//                                    System.out.println("checkListsize" + checkedList.size());
//                                }
                            }
                            if (checkamount >= 3) {
                                Toast.makeText(VerifyLogin.this, "最多选择三项", Toast.LENGTH_SHORT).show();
                            }
                        }
                });
                break;
            }
            case "1": {
                idArrayList = (ArrayList<VerifyIdBean.SellectListBean>) intent.getSerializableExtra("idList");
                detailRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));

                verifyIdRecycleViewAdapter = new VerifyIdRecycleViewAdapter(this, idArrayList);
                detailRecyclerView.setAdapter(verifyIdRecycleViewAdapter);

                ItemClickSupport.addTo(detailRecyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {

                        idArrayList.get(position).setCheckedFlag(!idArrayList.get(position).getCheckedFlag());

                        System.out.println("name:" + idArrayList.get(position).getAccountname() + "flag" + idArrayList.get(position).getCheckedFlag());

                        if (checkamount < 3) {
                            if (idArrayList.get(position).getCheckedFlag() == true) {
                                v.setBackgroundResource(R.drawable.encryption_choice);
                                checkedNameList.add(idArrayList.get(position).getAccountname());
                                checkedAcountList.add(idArrayList.get(position).getAccount());
                                checkamount++;
                            } else {
                                v.setBackgroundColor(Color.WHITE);
                                checkedNameList.remove(idArrayList.get(position).getAccountname());
                                checkedAcountList.remove(idArrayList.get(position).getAccount());
                                checkamount--;
                            }
                            Toast.makeText(VerifyLogin.this, "已选择" + checkedAcountList.size() + "项,最多选择三项", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(VerifyLogin.this, "最多选择三项", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;
            }
        }
    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {

    }

    @Override
    public void setComponet() {
        DaggerVerifyAcountComponet.builder()
                .verifyAcountPresenterModule(new VerifyAcountPresenterModule(this))
                .build()
                .in(this);
    }


    @OnClick(R.id.btn_verify)
    public void onViewClicked() {
        cid = PushManager.getInstance().getClientid(this);
        token = EnterPage.enterphonenumber;
        switch (index) {
            case "0": {
                data = getData();
                if (data == "") {
                    Toast.makeText(this, "请至少选择一项", Toast.LENGTH_SHORT).show();
                } else {
                    System.out.println("flag:" + flag + " data:" + data + " token:" + token + " cid:" + cid);
                    verifyAccountPresenter.verifyLogin(flag, data, token, cid);
                }

                break;
            }
            case "1": {
                data = getIdData();
                if (data == "") {
                    Toast.makeText(this, "请至少选择一项", Toast.LENGTH_SHORT).show();
                } else {
                    verifyAccountPresenter.verifyLogin(flag, data, token, cid);
                }
            }
        }


    }

    private String getIdData() {
        for (int i = 0; i < checkedAcountList.size(); i++) {
            data = data + checkedNameList.get(i) + "," + checkedAcountList.get(i) + "#";
        }
        return data;
    }

    public String getData() {
        for (int i = 0; i < checkedList.size(); i++) {
            data = data + checkedList.get(i) + "#";
        }
        return data;
    }
}
